#include <krnlTypes.h>
#include "drvIOConfig.h"

void drvSPIInit(void)
{
	InitSCK( OUTPUT_PIN );
	InitMOSI( OUTPUT_PIN );
	InitMISO( INPUT_PIN );
}

///////////////////////////////////////////////////////////////////////////////
// Send and receive byte on software SPI
dosByte drvSPISendAndReceiveByte( dosByte in_data )
{
	dosByte i;
	dosByte port_data;
	dosByte data_read = 0;

	// SCK, MOSI
	SPISCK( 0 );
	SPIMOSI( 0 );

	for( i = 0; i<8; i++ )
	{
		// send data bit to data out
		if( (in_data & (1<<7)) == 0 )
			SPIMOSI( 0 );
		else
			SPIMOSI( 1 );
			
		in_data <<= 1;	
		SPIClockDelay();

		// rising edge of clock
		SPISCK( 1 );
		SPIClockDelay();

		// read bit
		data_read <<= 1;
		if( SPIMISO() == 1 )
			data_read |= 1;

		// falling edge of clock
		SPISCK( 0 );
	}

	return data_read;
}

